/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.list;

import com.openwaf.client.core.Window;
import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.InputElement;
import com.openwaf.client.dom.TableCellElement;
import com.openwaf.client.dom.TableElement;
import com.openwaf.client.dom.TableRowElement;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.event.logical.SelectionChangeEvent;
import com.openwaf.client.ui.UIControl;

public class RadioList extends AbstractList{

    private int selectedIndex;

    public RadioList(){
        selectedIndex=-1;
    }
    @Override
    public void addElement(String elm){
        insertRowAt(elements.size()).getCells()[1].appendChild(Window.getDocument().createTextNode(elm));
        elements.add(elm);
    }

    @Override
    public void addElement(Element elm) {
        insertRowAt(elements.size()).getCells()[1].appendChild(elm);
        elements.add(elm);
    }

    @Override
    public void addElement(UIControl elm) {
        insertRowAt(elements.size()).getCells()[1].appendChild(elm.getElement());
        elements.add(elm);
    }
    @Override
    public void addElement(int index,String elm){
        insertRowAt(index).getCells()[1].appendChild(Window.getDocument().createTextNode(elm));
        elements.add(elm);
    }
    @Override
    public void addElement(int index, Element elm) {
        insertRowAt(index).getCells()[1].appendChild(elm);
        elements.add(elm);
    }

    @Override
    public void addElement(int index, UIControl elm) {
        insertRowAt(index).getCells()[1].appendChild(elm.getElement());
        elements.add(elm);
    }

    private void onRadioClick(InputElement element){
        select(((TableRowElement)element.getParentNode().getParentNode()).getRowIndex());
        if(eventmanager!=null){
            if(eventmanager.hasEventHandlersForType(SelectionChangeEvent.NAME)){
                SelectionChangeEvent event=new SelectionChangeEvent(this);
                eventmanager.fireEvent(SelectionChangeEvent.NAME, event);
            }
        }
    }
    @Override
    public boolean isSelected(int index){
        checkIndex(index);
        return index==selectedIndex;
    }

    public int getSelectedIndex(){
        return selectedIndex;
    }
       
    public void select(int index){
        checkIndex(index);        
        if(selectedIndex>=0 && selectedIndex<elements.size()){
            deselect(selectedIndex);
        }
        ((InputElement)tblRoot.getRows()[index].getCells()[0].getFirstChild()).setChecked(true);
        tblRoot.getRows()[index].setCssClassName(CSS_SELECTED);
        selectedIndex=index;

    }
    public void deselect(int index){
        checkIndex(index);
        if(index==selectedIndex){
            selectedIndex=-1;            
            ((InputElement)tblRoot.getRows()[index].getCells()[0].getFirstChild()).setChecked(false);
            tblRoot.getRows()[index].setCssClassName(CSS_NORMAL);
        }        
    }
    private void onCellClick(TableRowElement tr){
        select(tr.getRowIndex());
        if(eventmanager!=null){
            if(eventmanager.hasEventHandlersForType(SelectionChangeEvent.NAME)){
                SelectionChangeEvent event=new SelectionChangeEvent(this);
                eventmanager.fireEvent(SelectionChangeEvent.NAME, event);
            }
        }
    }
    private TableRowElement insertRowAt(int index){
        if(index<0 && index>elements.size()){
            throw new IndexOutOfBoundsException("Index out of range. index:"+index);
        }
        final TableRowElement tr=tblRoot.insertRow(index);
        TableCellElement tc=tr.insertCell(0);
        tc.getStyle().setWidth("20px");
        final InputElement inp=(InputElement)Window.getDocument().createElement(InputElement.TAG);
        inp.setType(InputElement.InputType.RADIO);
        inp.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                onRadioClick(inp);
            }
        });
        tc.appendChild(inp);
        tc=tr.insertCell(1);
        tc.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                onCellClick(tr);
            }
        });
        tr.setCssClassName(CSS_NORMAL);
        return tr;
    }

}
